const moment = require('moment')

function costUnit(unit) {
	switch (unit) {
		case 'PerManDay':
			return '/人天'
		case 'PerManHour':
			return '/人时'
		case 'PerManMonth':
			return '/人月'
		default:
			return ""
	}
}
module.exports = { 
	age(birthday){
		return Math.ceil((moment().diff(moment(birthday), 'month') / 12))
	},

	isNew(createdDate) {
		const timeDifference = moment().diff(moment(createdDate), 'month')
		if (timeDifference >= 3) {
			return false
		}
		return moment(createdDate).isBefore()
	},

	experience(startDate) {
		return Math.ceil((moment().diff(moment(startDate), 'month') / 1.2)) / 10
	},

	sortByDate(a, b) {
		return moment(a.effectiveDate).diff(moment(b.effectiveDate), 'day')
	},

	sortDate(a, b) {
		if (a.availability.availableDate === b.availability.availableDate) {
			return b.profession.experience - a.profession.experience
		}
		else {
			return moment(a.availability.availableDate).diff(moment(b.availability.availableDate), 'day')
		}
	},

	currentCost(plans) {
		const effectivePlans = plans.filter((plan) => {
			return moment(plan.effectiveDate).isSameOrBefore(moment(), 'day')
		}).sort(this.sortByDate).map(plan => plan.amount)
		return effectivePlans.length > 0 ? effectivePlans[effectivePlans.length - 1] : -1
	},


	costDescription(amount, unit) {
		let u = costUnit(unit)
		return amount > 0 ? `${amount}元${u}` : "暂无报价"
	},
	

	

	availableDescription(available) {
		let msg = ""
		if (new Date(available) < new Date()) {
			msg = "随时"
		} else {
			let _available = new Date(available);
			let month = _available.getMonth() + 1;
			let day = _available.getDate();
			if (day >= 1 && day <= 10) {
				msg = `${month}月上旬`
			} else {
				if (day >= 11 && day <= 19) {
					msg = `${month}月中旬`
				} else {
					msg = `${month}月下旬`
				}
			}

			if (moment(available).diff(moment(), 'day') <= 14) {
				msg = '两周内';
			}
		}
		return msg + "可入场";
	},

	extractSkillTags(profiles) {
		if(!profiles) return []
		
		return [... new Set(profiles.reduce((tags, profile) => {
			return tags.concat(profile.profession.tags)
		}, []))]
	}
}